<template>
  <!-- 详情页 -->
  <!--
      模板首次渲染使用初始值，当我们访问某个默认值为null或者undefined的属性的时候会报错
      通过v-if手动控制模板渲染时机，只有当属性是存在或者有值的时候才正式渲染模板(正式读取属性)
     -->
  <div class='xtx-goods-page' v-if="goodsData.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsData.categories[1].id}`">
          {{ goodsData.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/subCategory/${goodsData.categories[0].id}`">
          {{ goodsData.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem to="#">{{ goodsData.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
            <GoodsImage :image-list="goodsData.mainPictures"/>
            <!-- 销量组件 -->
            <GoodsSales :goods="goodsData"/>
        </div>
        <!-- 右侧商品信息区 -->
        <div class="spec">
            <GoodsName :goods="goodsData"/>
            <!-- sku组件 -->
            <Sku :goods="goodsData"  @change="skuChange"></Sku>
            <!-- 数量组件 -->
            <XtxNumbox v-model="num"></XtxNumbox>
            <!-- 按钮组件 -->
            <XtxButton @click="insertCart" type="primary" style="margin-top: 10px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import Sku from './Sku'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import { useStore } from 'vuex'
import { message } from '@/components/Message/index.js'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodsName,
    Sku,
    GoodsTabs,
    GoodsHot
  },
  setup () {
    const route = useRoute()
    const goodsData = ref({})
    async function loadGoodsData () { // 商品数据
      const res = await findGoods(route.params.id)
      console.log('商品详情', res)
      goodsData.value = res.result
    }
    onMounted(() => {
      loadGoodsData()
    })

    const skuData = ref({}) // 抛出的sku数据对象
    function skuChange (sku) {
      console.log('sku', sku)
      if (sku?.skuId) {
        skuData.value = sku
      }
    }

    // 商品数量
    const num = ref(1)

    // 加入购物车逻辑
    const store = useStore()
    function insertCart () {
      // 1. 准备好加入购物车的所有字段数据
      const goods = {
        id: goodsData.value.id,
        name: goodsData.value.name,
        picture: goodsData.value.mainPictures[0],
        skuId: skuData.value.skuId,
        price: skuData.value.oldPrice,
        nowPrice: skuData.value.price,
        attrsText: skuData.value.specsText,
        stock: skuData.value.inventory,
        selected: true, // 是否选中
        isEffective: true, // 是否有效
        count: num.value // 商品数据
      }

      // 2. 触发已经准备好的action函数，把数据当成实参传递过去
      if (goods.skuId) { // skuId有值，是有效的sku，再执行添加逻辑
        store.dispatch('cart/asyncInsertCart', goods).then(() => {
          message({ type: 'success', text: '添加购物车成功' })
        })
      } else {
        message({ type: 'error', text: '请选择商品规格' })
      }
    }

    return { goodsData, skuChange, num, insertCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

// 商品信息区
.goods-info {
    min-height: 600px;
    background: #fff;
    display: flex;
    .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
    }
    .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
    }
}
</style>
